import { getToken, setToken, removeToken } from '@/utils/auth'
import { login, getUserInfo } from '@/api/user'
const state = {
  token: getToken(),
  userInfo: {}
}
const mutations = {
  updateToken(state, token) {
    state.token = token
    setToken(token)
  },
  delToken(state) {
    state.token = null
    removeToken()
  },
  updateUserInfo(state, payload) {
    state.userInfo = payload
  }
}
const actions = {
  async login(context, data) {
    console.log('登录', data)
    const res = await login(data)
    // console.log(res)
    context.commit('updateToken', res)
    // context.commit('updateToken', '1122333')
  },
  async getuserinfo(context) {
    const res = await getUserInfo()
    context.commit('updateUserInfo', res)
  },
  logout(state) {
    state.commit('delToken')
    state.commit('updateUserInfo', {})
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
